package com.mall.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.result.Result;
import com.mall.system.entity.Dept;
import com.mall.system.service.DeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门控制器
 */
@Tag(name = "部门管理")
@Slf4j
@RestController
@RequestMapping("/system/depts")
public class DeptController {
    
    @Autowired
    private DeptService deptService;
    
    @Operation(summary = "部门列表")
    @GetMapping
    public Result<IPage<Dept>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Dept dept) {
        try {
            IPage<Dept> page = new Page<>(pageNum, pageSize);
            QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
            
            // 添加查询条件
            if (dept.getDeptName() != null && !dept.getDeptName().isEmpty()) {
                queryWrapper.like("dept_name", dept.getDeptName());
            }
            if (dept.getStatus() != null) {
                queryWrapper.eq("status", dept.getStatus());
            }
            
            IPage<Dept> result = deptService.page(page, queryWrapper);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询部门列表异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "根据部门ID获取部门")
    @GetMapping("/{deptId}")
    public Result<Dept> getById(@PathVariable("deptId") Long deptId) {
        try {
            Dept dept = deptService.getById(deptId);
            if (dept != null) {
                return Result.success(dept);
            } else {
                return Result.failed("部门不存在");
            }
        } catch (Exception e) {
            log.error("查询部门异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "新增部门")
    @PostMapping
    public Result<Boolean> add(@RequestBody Dept dept) {
        try {
            boolean success = deptService.save(dept);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("新增部门失败");
            }
        } catch (Exception e) {
            log.error("新增部门异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "更新部门")
    @PutMapping
    public Result<Boolean> update(@RequestBody Dept dept) {
        try {
            if (dept.getDeptId() == null) {
                return Result.failed("部门ID不能为空");
            }
            
            boolean success = deptService.updateById(dept);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("更新部门失败");
            }
        } catch (Exception e) {
            log.error("更新部门异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "删除部门")
    @DeleteMapping("/{ids}")
    public Result<Boolean> delete(@PathVariable("ids") List<Long> ids) {
        try {
            boolean success = deptService.removeByIds(ids);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("删除部门失败");
            }
        } catch (Exception e) {
            log.error("删除部门异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "获取所有部门列表")
    @GetMapping("/list")
    public Result<List<Dept>> listAll() {
        try {
            QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", 1);
            queryWrapper.eq("del_flag", 0);
            queryWrapper.orderByAsc("order_num");
            
            List<Dept> depts = deptService.list(queryWrapper);
            return Result.success(depts);
        } catch (Exception e) {
            log.error("获取部门列表异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
}